<template>
  <d2-container>
    <template slot="header">用户列表</template>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-button type="success" size="mini" @click="handleCreate">新增用户</el-button>
      </el-form-item>
    </el-form>
    <template>
      <el-table
        :data="tableData"
        style="width: 100%"
        max-height="400">
        <el-table-column
          fixed
          prop="id"
          label="编号"
          width="150">
        </el-table-column>
        <el-table-column
          prop="userName"
          label="姓名"
          width="120">
        </el-table-column>
        <el-table-column
          prop="departMent"
          label="部门"
          :formatter="formatDepartType"
          width="120">
        </el-table-column>
        <el-table-column
          prop="sex"
          label="性别"
          width="120">
        </el-table-column>
        <el-table-column
          prop="mobilePhone"
          label="手机号"
          width="300">
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="contract(scope.$index)"
              type="text"
              size="small">
              合同
            </el-button>
            <el-button
              @click.native.prevent="editRow(scope.$index, tableData)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.$index, tableData)"
              type="text"
              size="small">
              移除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="20"
        layout="total, prev, pager, next, jumper"
        :total="count">
      </el-pagination>
    </div>
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="90px">
        <el-col :span="12">
          <el-form-item label="部门类型">
            <i class="el-icon-edit"></i>`
            <el-select v-model="form.departId" placeholder="" size="mini">
              <el-option
                v-for="item in departMentList"
                :key="item.departId"
                :label="item.departName"
                :value=item.departId
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="账号">
            <el-input v-model="form.loginName" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="密码">
            <el-input v-model="form.password" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名">
            <el-input v-model="form.userName" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别">
            <el-select v-model="form.sex" placeholder="" size="mini">
              <el-option key="1" label="男" value="1"></el-option>
              <el-option key="2" label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="出生年月">
            <el-date-picker
              v-model="form.birthDate"
              type="date"
              size="mini"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="籍贯">
            <el-input v-model="form.birthPlace" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身体情况">
            <el-select v-model="form.physicalCondition" placeholder="" size="mini">
              <el-option label="健康" value="1"></el-option>
              <el-option label="非健康" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="手机号">
            <el-input v-model="form.mobilePhone" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="家庭住址">
            <el-input v-model="form.address" placeholder="" size="mini"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose()" size="mini">取 消</el-button>
    <el-button type="primary" @click="handleSubmit()" size="mini">确 定</el-button>
  </span>
    </el-dialog>
  </d2-container>
</template>
<script>
import api from '@/api'

export default {
  name: 'user',
  created () {
    this.getDepartMentList()
    this.getdata()
  },
  methods: {
    formatDepartType (row, column) {
      var departMap = {}
      for (var departMent of this.departMentList) {
        departMap[departMent.departId] = departMent.departName
      }
      return departMap[row[column.property]]
    },
    async getDepartMentList () {
      var res = await api.OA_DEPARTMEN_LIST({
        pageNo: 1,
        pageSize: 500
      })
      this.departMentList = res.list
      this.form.departId = res.list[0].departId
    },
    async getdata () {
      var res = await api.OA_USER_LIST({
        pageNo: this.currentPage,
        pageSize: 10
      })
      this.count = res.count
      this.tableData = res.list
    },
    handleCurrentChange (val) {
      this.currentPage = val
      console.log(`当前页: ${val}`)
    },
    contract () {
      this.$router.push({ name: 'market-customer-contract' })
    },
    handleCreate () {
      this.dialogVisible = true
      this.initForm()
    },
    handleClose () {
      this.dialogVisible = false
      this.initForm()
    },
    handleSubmit () {
      if (this.form.id) {
        api.OA_USER_EDIT(this.form)
        this.handleClose()
        this.initForm()
        this.getData()
      } else {
        delete this.form.id
        api.OA_USER_CRATE(this.form).then(() => {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.handleClose()
          this.getData()
        })
      }
    },
    handleEdit (index, row) {
      this.dialogTitle = '编辑用户'
      this.dialogVisible = true
      this.form.departId = row.departId
    },
    handelDelete (index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.OA_USER_DELETE()
        this.getData()
      })
    },
    initForm () {
      this.form.departId = 1
    }
  },
  data () {
    return {
      departMentList: [],
      dialogTitle: '新增用户',
      dialogVisible: false,
      currentPage: 1,
      count: 0,
      tableData: [],
      form: {
        id: 0,
        departId: 1,
        loginName: '',
        password: '',
        userName: '',
        sex: '1',
        birthDate: '',
        birthPlace: '',
        physicalCondition: '1',
        mobilePhone: '',
        address: ''
      }
    }
  }
}
</script>
